﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;

namespace Anata.WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public int Interval { get; set; }

        int row_id;

        #region Test Services

        public object TestService(object value)
        {
            return "Object : " + value;
        }

        public List<object> TestServiceList(List<object> value)
        {
            List<object> list = new List<object>();

            list.Add("Hello,");

            foreach (var item in value)
            {
                list.Add(item);
            }

            list.Add("Bye.");

            return list;
        }

        public DataSet TestPullDataset(DateTime lastpull)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[5];

            var query = from row in context.Agama where row.LastUpdated > lastpull select row;
            foreach (Agama item in query.ToList())
            {
                listofobjectarray[0] = (item.IdAgama);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.IdUser);
                listofobjectarray[3] = (item.LastUpdated);
                listofobjectarray[4] = (item.Kode);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);

            return ds;
        }

        #endregion

        #region Pull Services

        /*
        	Input: lastpull, waktu terakhir kali client melakukan penarikan data, id_cabang -> integer PK cabang
        	Output: struktur data yang bersesuaian dengan tabel yang diminta
        	Deskripsi: mengquery database terpusat pada tabel dan mengambil data yang ‘lastUpdated’ nya lebih baru daripada ‘lastPull’.
        */

        public DataSet PullAgama(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[5];

            var query = from row in context.Agama where row.LastUpdated > lastpull select row;
            foreach (Agama item in query.ToList())
            {
                listofobjectarray[0] = (item.IdAgama);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.IdUser);
                listofobjectarray[3] = (item.LastUpdated);
                listofobjectarray[4] = (item.Kode);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullCabangLayanan(DateTime lastpull, int id_cabang)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[5];

            var query = from row in context.CabangLayanan where row.LastUpdated > lastpull && row.IdCabang == id_cabang select row;
            foreach (CabangLayanan item in query.ToList())
            {
                listofobjectarray[0] = (item.IdCabang);
                listofobjectarray[1] = (item.IdLayanan);
                listofobjectarray[2] = (item.HargaLayanan);
                listofobjectarray[3] = (item.IdUser);
                listofobjectarray[4] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullCabangProduk(DateTime lastpull, int id_cabang)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[5];

            var query = from row in context.CabangProduk where row.LastUpdated > lastpull && row.IdCabang == id_cabang select row;
            foreach (CabangProduk item in query.ToList())
            {
                listofobjectarray[0] = (item.IdCabang);
                listofobjectarray[1] = (item.IdJenisStok);
                listofobjectarray[2] = (item.HargaProduk);
                listofobjectarray[3] = (item.IdUser);
                listofobjectarray[4] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullFreeGift(DateTime lastpull, int id_cabang)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[9];

            var query = from row in context.FreeGift where row.LastUpdated > lastpull && row.IdCabang == id_cabang select row;
            foreach (FreeGift item in query.ToList())
            {
                listofobjectarray[0] = (item.IdFreeGift);
                listofobjectarray[1] = (item.IdLayanan);
                listofobjectarray[2] = (item.IdCabang);
                listofobjectarray[3] = (item.Nama);
                listofobjectarray[4] = (item.IdJenisStok);
                listofobjectarray[5] = (item.Jumlah);
                listofobjectarray[6] = (item.IsAktif);
                listofobjectarray[7] = (item.IdUser);
                listofobjectarray[8] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullGolonganDarah(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[4];

            var query = from row in context.GolonganDarah where row.LastUpdated > lastpull select row;
            foreach (GolonganDarah item in query.ToList())
            {
                listofobjectarray[0] = (item.IdGolonganDarah);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.IdUser);
                listofobjectarray[3] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullJenisAset(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[4];

            var query = from row in context.JenisAset where row.LastUpdated > lastpull select row;
            foreach (JenisAset item in query.ToList())
            {
                listofobjectarray[0] = (item.IdJenisAset);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.IdUser);
                listofobjectarray[3] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullJenisPembayaran(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[5];

            var query = from row in context.JenisPembayaran where row.LastUpdated > lastpull select row;
            foreach (JenisPembayaran item in query.ToList())
            {
                listofobjectarray[0] = (item.IdJenisPembayaran);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.IsTunai);
                listofobjectarray[3] = (item.IdUser);
                listofobjectarray[4] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullJenisStok(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[4];

            var query = from row in context.JenisStok where row.LastUpdated > lastpull select row;
            foreach (JenisStok item in query.ToList())
            {
                listofobjectarray[0] = (item.IdJenisStok);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.IdUser);
                listofobjectarray[3] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullKaryawan(DateTime lastpull, int id_cabang)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[15];

            var query = from row in context.Karyawan where row.LastUpdated > lastpull && row.IdCabang == id_cabang select row;
            foreach (Karyawan item in query.ToList())
            {
                listofobjectarray[0] = (item.IdKaryawan);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.Alamat);
                listofobjectarray[3] = (item.NoTelepon);
                listofobjectarray[4] = (item.NoKTP);
                listofobjectarray[5] = (item.IdJenisKelamin);
                listofobjectarray[6] = (item.TanggalLahir);
                listofobjectarray[7] = (item.IdStatus);
                listofobjectarray[8] = (item.IdAgama);
                listofobjectarray[9] = (item.IdGolonganDarah);
                listofobjectarray[10] = (item.TanggalMasuk);
                listofobjectarray[11] = (item.TanggalKeluar);
                listofobjectarray[12] = (item.IdCabang);
                listofobjectarray[13] = (item.IdUser);
                listofobjectarray[14] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullKaryawanLayanan(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[5];

            var query = from row in context.KaryawanLayanan where row.LastUpdated > lastpull select row;
            foreach (KaryawanLayanan item in query.ToList())
            {
                listofobjectarray[0] = (item.IdKaryawan);
                listofobjectarray[1] = (item.IdLayanan);
                listofobjectarray[2] = (item.Komisi);
                listofobjectarray[3] = (item.IdUser);
                listofobjectarray[4] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullKonsumen(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[8];

            var query = from row in context.Konsumen where row.LastUpdated > lastpull select row;
            foreach (Konsumen item in query.ToList())
            {
                listofobjectarray[0] = (item.IdKonsumen);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.Alamat);
                listofobjectarray[3] = (item.NoTelepon);
                listofobjectarray[4] = (item.JenisKelamin);
                listofobjectarray[5] = (item.TanggalLahir);
                listofobjectarray[6] = (item.IdUser);
                listofobjectarray[7] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullLayanan(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[5];

            var query = from row in context.Layanan where row.LastUpdated > lastpull select row;
            foreach (Layanan item in query.ToList())
            {
                listofobjectarray[0] = (item.IdLayanan);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.Durasi);
                listofobjectarray[3] = (item.IdUser);
                listofobjectarray[4] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullPromo(DateTime lastpull, int id_cabang)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[17];

            var query = from row in context.Promo where row.LastUpdated > lastpull && row.IdCabang == id_cabang select row;
            foreach (Promo item in query.ToList())
            {
                listofobjectarray[0] = (item.IdPromo);
                listofobjectarray[1] = (item.IdLayanan);
                listofobjectarray[2] = (item.IdCabang);
                listofobjectarray[3] = (item.Nama);
                listofobjectarray[4] = (item.IsPersentase);
                listofobjectarray[5] = (item.PersentasePromo);
                listofobjectarray[6] = (item.NominalPromo);
                listofobjectarray[7] = (item.Syarat);
                listofobjectarray[8] = (item.IsOnce);
                listofobjectarray[9] = (item.WaktuMulai);
                listofobjectarray[10] = (item.WaktuSelesai);
                listofobjectarray[11] = (item.IsDaily);
                listofobjectarray[12] = (item.IsWeekly);
                listofobjectarray[13] = (item.IsMonthly);
                listofobjectarray[14] = (item.IsYearly);
                listofobjectarray[15] = (item.IdUser);
                listofobjectarray[16] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullRole(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[4];

            var query = from row in context.Role where row.LastUpdated > lastpull select row;
            foreach (Role item in query.ToList())
            {
                listofobjectarray[0] = (item.IdRole);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.IdUser);
                listofobjectarray[3] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet PullStatus(DateTime lastpull)
        {
            AnataEntities context = new AnataEntities();
            List<object[]> list = new List<object[]>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] listofobjectarray = new object[4];

            var query = from row in context.Status where row.LastUpdated > lastpull select row;
            foreach (Status item in query.ToList())
            {
                listofobjectarray[0] = (item.IdStatus);
                listofobjectarray[1] = (item.Nama);
                listofobjectarray[2] = (item.IdUser);
                listofobjectarray[3] = (item.LastUpdated);
                list.Add((object[])listofobjectarray.Clone());
            }

            context.Dispose();

            for (int i = 0; i < listofobjectarray.Length; ++i)
            {
                dt.Columns.Add();
            }

            foreach (var a in list)
            {
                dt.Rows.Add(a);
            }

            ds.Tables.Add(dt);
            return ds;
        }

        #endregion

        #region Push Services

        /*
        ?	Input: struktur data Transaksi yang serializable dan dapat dikirim melalui jaringan
        ?	Output: integer yang merupakan id baru yang sinkron dengan id pada tabel terpusat
        ?	Deskripsi: menambah satu row baru di tabel transaksi terpusat, dan mengembalikan id yang dihasilkan 
        */

        public int PushKaryawanKehadiran(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            KaryawanKehadiran data = new KaryawanKehadiran();

            if (objectarray[1] != System.DBNull.Value) data.IdKaryawan = Int32.Parse(objectarray[1].ToString());
            if (objectarray[2] != System.DBNull.Value) data.WaktuMasuk = DateTime.Parse(objectarray[2].ToString());
            if (objectarray[3] != System.DBNull.Value) data.WaktuKeluar = DateTime.Parse(objectarray[3].ToString());
            if (objectarray[4] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[4].ToString());
            if (objectarray[5] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[5].ToString());
            if (objectarray[6] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[6].ToString());
            if (objectarray[7] != System.DBNull.Value) data.HasBeenPushedTS = DateTime.Parse(objectarray[7].ToString());
            if (objectarray[8] != System.DBNull.Value) data.IsIjin = Boolean.Parse(objectarray[8].ToString());
            if (objectarray[9] != System.DBNull.Value) data.IsLembur = Boolean.Parse(objectarray[9].ToString());

            context.KaryawanKehadiran.AddObject(data);
            context.SaveChanges();

            row_id = data.IdKaryawanKehadiran;
            context.Dispose();

            //var query = realcontext.KaryawanKehadiran.First(
            //    row => row.IdKaryawan == data.IdKaryawan &&
            //        row.WaktuMasuk == data.WaktuMasuk &&
            //        row.WaktuKeluar == data.WaktuKeluar &&
            //        row.HasBeenPushedTS == data.HasBeenPushedTS);

            //row_id = query.IdKaryawanKehadiran;

            //var query = from row in realcontext.KaryawanKehadiran
            //            where row.IdKaryawan == data.IdKaryawan && row.WaktuMasuk == data.WaktuMasuk &&
            //            row.WaktuKeluar == data.WaktuKeluar && row.HasBeenPushedTS == data.HasBeenPushedTS
            //            select row;

            //row_id = query.ToList()[0].IdKaryawanKehadiran;

            //AnataEntities temp = new AnataEntities();

            //row_id = temp.KaryawanKehadiran.First(
            //    row => row.IdKaryawan == data.IdKaryawan &&
            //        row.WaktuMasuk == data.WaktuMasuk &&
            //        row.WaktuKeluar == data.WaktuKeluar &&
            //        row.HasBeenPushedTS == data.HasBeenPushedTS
            //        ).IdKaryawanKehadiran;

            //temp.Dispose();

            return row_id;
        }

        public int PushKonsumen(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            Konsumen data = new Konsumen();

            if (objectarray[0] != System.DBNull.Value) data.IdKonsumen = Int32.Parse(objectarray[0].ToString());
            if (objectarray[1] != System.DBNull.Value) data.Nama = objectarray[1].ToString();
            if (objectarray[2] != System.DBNull.Value) data.Alamat = objectarray[2].ToString();
            if (objectarray[3] != System.DBNull.Value) data.NoTelepon = objectarray[3].ToString();
            if (objectarray[4] != System.DBNull.Value) data.JenisKelamin = objectarray[4].ToString();
            if (objectarray[5] != System.DBNull.Value) data.TanggalLahir = DateTime.Parse(objectarray[5].ToString());
            if (objectarray[6] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[6].ToString());
            if (objectarray[7] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[7].ToString());
            if (objectarray[8] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[8].ToString());
            if (objectarray[9] != System.DBNull.Value) data.IdJenisKelamin = Int32.Parse(objectarray[9].ToString());

            context.Konsumen.AddObject(data);
            context.SaveChanges();

            row_id = data.IdKonsumen;
            context.Dispose();

            return row_id;
        }

        public int PushMutasiRekening(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            MutasiRekening data = new MutasiRekening();

            if (objectarray[00] != System.DBNull.Value) data.IdMutasiRekening = Int32.Parse(objectarray[0].ToString());
            if (objectarray[01] != System.DBNull.Value) data.NominalMutasi = Int32.Parse(objectarray[1].ToString());
            if (objectarray[02] != System.DBNull.Value) data.IsOtomatis = Boolean.Parse(objectarray[2].ToString());
            if (objectarray[03] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[3].ToString());
            if (objectarray[04] != System.DBNull.Value) data.WaktuMutasi = DateTime.Parse(objectarray[4].ToString());
            if (objectarray[05] != System.DBNull.Value) data.IdCabang = Int32.Parse(objectarray[5].ToString());
            if (objectarray[06] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[6].ToString());
            if (objectarray[07] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[7].ToString());
            if (objectarray[08] != System.DBNull.Value) data.HasBeenPushedTS = DateTime.Parse(objectarray[8].ToString());
            if (objectarray[09] != System.DBNull.Value) data.IdRekening = Int32.Parse(objectarray[9].ToString());
            if (objectarray[10] != System.DBNull.Value) data.IdJenisMutasiRekening = Int32.Parse(objectarray[10].ToString());
            if (objectarray[11] != System.DBNull.Value) data.Keterangan = objectarray[11].ToString();
            if (objectarray[12] != System.DBNull.Value) data.SaldoRekening = Int32.Parse(objectarray[0].ToString());

            context.MutasiRekening.AddObject(data);
            context.SaveChanges();

            row_id = data.IdMutasiRekening;
            context.Dispose();

            return row_id;
        }

        public int PushMutasiStok(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            MutasiStok data = new MutasiStok();

            if (objectarray[0] != System.DBNull.Value) data.IdMutasiStok = Int32.Parse(objectarray[0].ToString());
            if (objectarray[1] != System.DBNull.Value) data.JumlahMutasi = Int32.Parse(objectarray[1].ToString());
            if (objectarray[2] != System.DBNull.Value) data.Keterangan = objectarray[2].ToString();
            if (objectarray[3] != System.DBNull.Value) data.WaktuMutasi = DateTime.Parse(objectarray[3].ToString());
            if (objectarray[4] != System.DBNull.Value) data.IdStok = Int32.Parse(objectarray[4].ToString());
            if (objectarray[5] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[5].ToString());
            if (objectarray[6] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[6].ToString());
            if (objectarray[7] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[7].ToString());
            if (objectarray[8] != System.DBNull.Value) data.HasBeenPushedTS = DateTime.Parse(objectarray[8].ToString());
            if (objectarray[9] != System.DBNull.Value) data.IdJenisMutasiStok = Int32.Parse(objectarray[9].ToString());

            context.MutasiStok.AddObject(data);
            context.SaveChanges();

            row_id = data.IdMutasiStok;
            context.Dispose();
            
            return row_id;
        }

        public int PushStok(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            Stok data = new Stok();

            if (objectarray[00] != System.DBNull.Value) data.IdStok = Int32.Parse(objectarray[0].ToString());
            if (objectarray[01] != System.DBNull.Value) data.IdJenisStok = Int32.Parse(objectarray[1].ToString());
            if (objectarray[02] != System.DBNull.Value) data.IdCabang = Int32.Parse(objectarray[2].ToString());
            if (objectarray[03] != System.DBNull.Value) data.JumlahBeli = Int32.Parse(objectarray[3].ToString());
            if (objectarray[04] != System.DBNull.Value) data.WaktuBeli = DateTime.Parse(objectarray[4].ToString());
            if (objectarray[05] != System.DBNull.Value) data.TanggalKadaluarsa = DateTime.Parse(objectarray[5].ToString());
            if (objectarray[06] != System.DBNull.Value) data.Catatan = objectarray[6].ToString();
            if (objectarray[07] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[7].ToString());
            if (objectarray[08] != System.DBNull.Value) data.WaktuUpdate = DateTime.Parse(objectarray[8].ToString());
            if (objectarray[09] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[9].ToString());
            if (objectarray[10] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[10].ToString());
            if (objectarray[11] != System.DBNull.Value) data.HasBeenPushedTS = DateTime.Parse(objectarray[11].ToString());
            if (objectarray[12] != System.DBNull.Value) data.HargaBeli = Int32.Parse(objectarray[12].ToString());
            if (objectarray[13] != System.DBNull.Value) data.SumJumlahMutasiStok = Int32.Parse(objectarray[13].ToString());

            context.Stok.AddObject(data);
            context.SaveChanges();

            row_id = data.IdStok;
            context.Dispose();

            return row_id;
        }

        public int PushTransaksi(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            Transaksi data = new Transaksi();

            if (objectarray[00] != System.DBNull.Value) data.IdTransaksi = Int32.Parse(objectarray[0].ToString());
            if (objectarray[01] != System.DBNull.Value) data.WaktuBuat = DateTime.Parse(objectarray[1].ToString());
            if (objectarray[02] != System.DBNull.Value) data.WaktuSelesai = DateTime.Parse(objectarray[2].ToString());
            if (objectarray[03] != System.DBNull.Value) data.TotalHarga = Int32.Parse(objectarray[3].ToString());
            if (objectarray[04] != System.DBNull.Value) data.PotonganHarga = Int32.Parse(objectarray[4].ToString());
            if (objectarray[05] != System.DBNull.Value) data.KoreksiHarga = Int32.Parse(objectarray[5].ToString());
            if (objectarray[06] != System.DBNull.Value) data.IdKonsumen = Int32.Parse(objectarray[6].ToString());
            if (objectarray[07] != System.DBNull.Value) data.IdCabang = Int32.Parse(objectarray[7].ToString());
            if (objectarray[08] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[8].ToString());
            if (objectarray[09] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[9].ToString());
            if (objectarray[10] != System.DBNull.Value) data.IsBatal = Boolean.Parse(objectarray[10].ToString());
            if (objectarray[11] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[11].ToString());
            if (objectarray[12] != System.DBNull.Value) data.HasBeenPushedTS = DateTime.Parse(objectarray[12].ToString());
            if (objectarray[13] != System.DBNull.Value) data.WaktuBatal = DateTime.Parse(objectarray[13].ToString());

            context.Transaksi.AddObject(data);
            context.SaveChanges();

            row_id = data.IdTransaksi;
            context.Dispose();

            return row_id;
        }

        public int PushTransaksiLayanan(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            TransaksiLayanan data = new TransaksiLayanan();

            if (objectarray[00] != System.DBNull.Value) data.IdTransaksiLayanan = Int32.Parse(objectarray[0].ToString());
            if (objectarray[01] != System.DBNull.Value) data.IdTransaksi = Int32.Parse(objectarray[1].ToString());
            if (objectarray[02] != System.DBNull.Value) data.IdLayanan = Int32.Parse(objectarray[2].ToString());
            if (objectarray[03] != System.DBNull.Value) data.IdKaryawan = Int32.Parse(objectarray[3].ToString());
            if (objectarray[04] != System.DBNull.Value) data.HargaLayanan = Int32.Parse(objectarray[4].ToString());
            if (objectarray[05] != System.DBNull.Value) data.PotonganPromo = Int32.Parse(objectarray[5].ToString());
            if (objectarray[06] != System.DBNull.Value) data.WaktuBuat = DateTime.Parse(objectarray[6].ToString());
            if (objectarray[07] != System.DBNull.Value) data.WaktuTunggu = DateTime.Parse(objectarray[7].ToString());
            if (objectarray[08] != System.DBNull.Value) data.WaktuMulai = DateTime.Parse(objectarray[8].ToString());
            if (objectarray[09] != System.DBNull.Value) data.WaktuSelesai = DateTime.Parse(objectarray[9].ToString());
            if (objectarray[10] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[10].ToString());
            if (objectarray[11] != System.DBNull.Value) data.IdJenisStok = Int32.Parse(objectarray[11].ToString());
            if (objectarray[12] != System.DBNull.Value) data.NamaPromo = objectarray[12].ToString();
            if (objectarray[13] != System.DBNull.Value) data.NamaFreeGift = objectarray[13].ToString();
            if (objectarray[14] != System.DBNull.Value) data.JumlahFreeGift = Int32.Parse(objectarray[14].ToString());
            if (objectarray[15] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[15].ToString());
            if (objectarray[16] != System.DBNull.Value) data.NilaiKepuasan = Int32.Parse(objectarray[16].ToString());
            if (objectarray[17] != System.DBNull.Value) data.HasBeenPushedTS = DateTime.Parse(objectarray[17].ToString());
            if (objectarray[18] != System.DBNull.Value) data.IsBatal = Boolean.Parse(objectarray[18].ToString());
            if (objectarray[19] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[19].ToString());
            if (objectarray[20] != System.DBNull.Value) data.WaktuBatal = DateTime.Parse(objectarray[17].ToString());
            if (objectarray[21] != System.DBNull.Value) data.IsLangganan = Boolean.Parse(objectarray[18].ToString());
            if (objectarray[22] != System.DBNull.Value) data.KomisiKaryawan = Int32.Parse(objectarray[11].ToString());

            context.TransaksiLayanan.AddObject(data);
            context.SaveChanges();

            row_id = data.IdTransaksiLayanan;
            context.Dispose();

            return row_id;
        }

        public int PushTransaksiPembayaran(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            TransaksiPembayaran data = new TransaksiPembayaran();

            if (objectarray[0] != System.DBNull.Value) data.IdTransaksiPembayaran = Int32.Parse(objectarray[0].ToString());
            if (objectarray[1] != System.DBNull.Value) data.IdTransaksi = Int32.Parse(objectarray[1].ToString());
            if (objectarray[2] != System.DBNull.Value) data.IdJenisPembayaran = Int32.Parse(objectarray[2].ToString());
            if (objectarray[3] != System.DBNull.Value) data.NominalPembayaran = Int32.Parse(objectarray[3].ToString());
            if (objectarray[4] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[4].ToString());
            if (objectarray[5] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[5].ToString());
            if (objectarray[6] != System.DBNull.Value) data.HasBeenPushedTS = DateTime.Parse(objectarray[6].ToString());
            if (objectarray[7] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[7].ToString());
            if (objectarray[8] != System.DBNull.Value) data.IdRekening = Int32.Parse(objectarray[8].ToString());

            context.TransaksiPembayaran.AddObject(data);
            context.SaveChanges();

            row_id = data.IdTransaksiPembayaran;
            context.Dispose();

            return row_id;
        }

        public int PushTransaksiProduk(DataSet ds)
        {
            AnataEntities context; AnataEntities realcontext = new AnataEntities(); context = realcontext;

            object[] objectarray = ds.Tables[0].Rows[0].ItemArray;
            TransaksiProduk data = new TransaksiProduk();

            if (objectarray[00] != System.DBNull.Value) data.IdTransaksiProduk = Int32.Parse(objectarray[0].ToString());
            if (objectarray[01] != System.DBNull.Value) data.IdTransaksi = Int32.Parse(objectarray[1].ToString());
            if (objectarray[02] != System.DBNull.Value) data.IdJenisStok = Int32.Parse(objectarray[2].ToString());
            if (objectarray[03] != System.DBNull.Value) data.IdKaryawan = Int32.Parse(objectarray[3].ToString());
            if (objectarray[04] != System.DBNull.Value) data.JumlahProduk = Int32.Parse(objectarray[4].ToString());
            if (objectarray[05] != System.DBNull.Value) data.HargaProduk = Int32.Parse(objectarray[5].ToString());
            if (objectarray[06] != System.DBNull.Value) data.PotonganHarga = Int32.Parse(objectarray[6].ToString());
            if (objectarray[07] != System.DBNull.Value) data.IdUser = Int32.Parse(objectarray[7].ToString());
            if (objectarray[08] != System.DBNull.Value) data.LastUpdated = DateTime.Parse(objectarray[8].ToString());
            if (objectarray[09] != System.DBNull.Value) data.HasBeenPushedTS = DateTime.Parse(objectarray[9].ToString());
            if (objectarray[10] != System.DBNull.Value) data.HasBeenPushed = Boolean.Parse(objectarray[10].ToString());
            if (objectarray[11] != System.DBNull.Value) data.IsBatal = Boolean.Parse(objectarray[11].ToString());
            if (objectarray[12] != System.DBNull.Value) data.WaktuBatal = DateTime.Parse(objectarray[12].ToString());
            if (objectarray[13] != System.DBNull.Value) data.KomisiKaryawan = Int32.Parse(objectarray[13].ToString());

            context.TransaksiProduk.AddObject(data);
            context.SaveChanges();

            row_id = data.IdTransaksiProduk;
            context.Dispose();

            return row_id;
        }

        #endregion

    }
}
